#include <avr/pgmspace.h>
#include "WProgram.h"

const byte TEST_MODE = 0;

const byte STATE_MAIN = 0;
const byte STATE_MENU = 1;
const byte STATE_DEVICE_SETUP = 2;
const byte STATE_SENSOR_SELECT = 3;
const byte STATE_TIME_SET = 4;
const byte STATE_TABLE_VIEW = 5;
const byte STATE_PREFERENCES_SETUP = 6;
const byte STATE_HISTORY_VIEW = 7;
const byte STATE_HISTORY_SETUP = 8;
const byte STATE_DATA_SELECT = 9;

const byte ALARM_OFF = 0;
const byte ALARM_DISPLAY = 1;
const byte ALARM_SOUND = 2;

const byte BUTTON_MENU = 1;
const byte BUTTON_UP = 3;
const byte BUTTON_DOWN = 2;

const byte MENU_MAIN = 0;
const byte MENU_HISTORY_SETUP = 1;
const byte MENU_PREFERENCES_SETUP = 2;
const byte MENU_TIME_SET = 3;
const byte MENU_DEVICE_SETUP = 4;
const byte NUMBER_OF_MENUS = MENU_DEVICE_SETUP+1;

const byte DEVICE_LADDNING_RETUR = 0;
const byte DEVICE_LADDNING_UT    = 1;
const byte DEVICE_TANK1          = 2;
const byte DEVICE_TANK2          = 3;
const byte DEVICE_TANK3          = 4;
const byte DEVICE_TANK4          = 5;
const byte DEVICE_TANK5          = 6;
const byte DEVICE_TANK6          = 7;
const byte DEVICE_TANK7          = 8;
const byte DEVICE_VARMVATTEN     = 9;
const byte DEVICE_VARME_RETUR    = 10;
const byte DEVICE_VARME_UT       = 11;
const byte DEVICE_INNE           = 12;
const byte DEVICE_UTE            = 13;
const byte NUMBER_OF_DEVICES     = DEVICE_UTE+1;

const byte VARIABLE_EFFECT        = 0;
const byte VARIABLE_LOAD          = 1;
const byte VARIABLE_TIME_EMPTY    = 2;
const byte NUMBER_OF_VARIABLES    = VARIABLE_TIME_EMPTY + 1;
const byte VARIABLE_ID_START      = 100;
const byte VARIABLE_ID_EFFECT     = VARIABLE_ID_START + VARIABLE_EFFECT;
const byte VARIABLE_ID_LOAD       = VARIABLE_ID_START + VARIABLE_LOAD;
const byte VARIABLE_ID_TIME_EMPTY = VARIABLE_ID_START + VARIABLE_TIME_EMPTY;

FLASH_ARRAY(byte, VARIABLE_DEVICE_TABLE,
    DEVICE_LADDNING_RETUR,
    DEVICE_LADDNING_UT,
    DEVICE_TANK1,
    DEVICE_TANK2,
    DEVICE_TANK3,
    DEVICE_TANK4,
    DEVICE_TANK5,
    DEVICE_TANK6,
    DEVICE_TANK7,
    DEVICE_VARMVATTEN,
    DEVICE_VARME_RETUR,
    DEVICE_VARME_UT,
    DEVICE_INNE,
    DEVICE_UTE,
    VARIABLE_ID_EFFECT,
    VARIABLE_ID_LOAD,
    VARIABLE_ID_TIME_EMPTY);

const byte MAX_NUMBER_OF_SOUND_EVENT = 10;

const int NUMBER_OF_HISTORY = 4;
const int TOTAL_HISTORY = 128*NUMBER_OF_HISTORY;
const int HISTORY_DATA_OFFSET = 0;
const int HISTORY_RESOLUTION_OFFSET = 1;
const int HISTORY_MAX_VALUE_OFFSET = 2;
const int HISTORY_MIN_VALUE_OFFSET = 3;
const int NUMBER_OF_HISTORY_SETTINGS = HISTORY_MIN_VALUE_OFFSET + 1;
const int HISTORY_RESERVED_EEPROM = 8;

const byte MAX_SENSORS = 20;

const int EEPROM_BACKLIGHT_OFFSET = 0;
const int EEPROM_SENSOR_OFFSET = 1;
const int EEPROM_SENSOR_SIZE = 9;
const int EEPROM_SOUND_EVENT_OFFSET = MAX_SENSORS*EEPROM_SENSOR_SIZE+EEPROM_SENSOR_OFFSET;
const int EEPROM_CONTRAST_OFFSET = EEPROM_SOUND_EVENT_OFFSET+MAX_NUMBER_OF_SOUND_EVENT;
const int EEPROM_HISTORY_OFFSET = EEPROM_CONTRAST_OFFSET + 1;
const int EEPROM_BREAK_TEMP = EEPROM_HISTORY_OFFSET + NUMBER_OF_HISTORY_SETTINGS*HISTORY_RESERVED_EEPROM;

const float ILLEGAL_TEMP = -127;
const byte MAX_DISPLAY_ROWS = 8;
const byte BACKLIGHT_STEPS = 10;
const unsigned long FADEOUT_MILLIS = 10000;
const byte FADE_SPEED = 1;
const unsigned long FADE_DELAY = 10;
const byte BACKLIGHT_PIN = 6;
const byte RESET_PIN = 9;
const byte BUZZER_PIN = 5;
const byte CHAR_HEIGHT = 8;
const byte CHAR_WIDTH = 6;
const byte MAX_CONTRAST = 40;
const byte MIN_CONTRAST = 1;
const byte NUMBER_OF_LOAD_HISTORY = 5;
const byte LOAD_HISTORY_RESOLUTION = 5;

const prog_char STRING_MAIN[] PROGMEM = "Main";
const prog_char STRING_HISTORY_SETUP[] PROGMEM = "History setup";
const prog_char STRING_PREFERENCES_SETUP[] PROGMEM = "Preferences setup";
const prog_char STRING_TIME_SET[] PROGMEM = "Time/date set";
const prog_char STRING_DEVICE_SETUP[] PROGMEM = "Device setup";
const prog_char STRING_BACK[] PROGMEM = "Back";
const prog_char STRING_CLEAR[] PROGMEM = "Clear";
const prog_char STRING_SLUT_OM[] PROGMEM = "Slut om";
const prog_char STRING_ELDAT[] PROGMEM = "Eldat";
const prog_char STRING_BACKLIGHT[] PROGMEM = "Backlight";
const prog_char STRING_CONTRAST[] PROGMEM = "Contrast";
const prog_char STRING_BREAK_TEMP[] PROGMEM = "Break temp";
const prog_char STRING_SOUND[] PROGMEM = "Sound";

const prog_char STRING_DEVICE_LADDNING_RETUR[] PROGMEM = "Lm in";
const prog_char STRING_DEVICE_LADDNING_UT[]    PROGMEM = "Lm ut";
const prog_char STRING_DEVICE_TANK1[]          PROGMEM = "Tank1";
const prog_char STRING_DEVICE_TANK2[]          PROGMEM = "Tank2";
const prog_char STRING_DEVICE_TANK3[]          PROGMEM = "Tank3";
const prog_char STRING_DEVICE_TANK4[]          PROGMEM = "Tank4";
const prog_char STRING_DEVICE_TANK5[]          PROGMEM = "Tank5";
const prog_char STRING_DEVICE_TANK6[]          PROGMEM = "Tank6";
const prog_char STRING_DEVICE_TANK7[]          PROGMEM = "Tank7";
const prog_char STRING_DEVICE_VARMVATTEN[]     PROGMEM = "Varmv";
const prog_char STRING_DEVICE_VARME_RETUR[]    PROGMEM = "Retur";
const prog_char STRING_DEVICE_VARME_UT[]       PROGMEM = "Matng";
const prog_char STRING_DEVICE_INNE[]           PROGMEM = "Inne ";
const prog_char STRING_DEVICE_UTE[]            PROGMEM = "Ute  ";

//Make sure the strings are in the same order as the device constants!!!
const prog_char* DEVICE_STRING_TABLE[] PROGMEM =
{
  STRING_DEVICE_LADDNING_RETUR,
  STRING_DEVICE_LADDNING_UT,
  STRING_DEVICE_TANK1,
  STRING_DEVICE_TANK2,
  STRING_DEVICE_TANK3,
  STRING_DEVICE_TANK4,
  STRING_DEVICE_TANK5,
  STRING_DEVICE_TANK6,
  STRING_DEVICE_TANK7,
  STRING_DEVICE_VARMVATTEN,
  STRING_DEVICE_VARME_RETUR,
  STRING_DEVICE_VARME_UT,
  STRING_DEVICE_INNE,
  STRING_DEVICE_UTE
};

const prog_char STRING_VARIABLE_EFFECT[]     PROGMEM = "Effkt";
const prog_char STRING_VARIABLE_LOAD[]       PROGMEM = "Laddn";
const prog_char STRING_VARIABLE_TIME_EMPTY[] PROGMEM = "Tomt ";

//Make sure the strings are in the same order as the variable constants!!!
const prog_char* VARIABLE_STRING_TABLE[] PROGMEM =
{
  STRING_VARIABLE_EFFECT,
  STRING_VARIABLE_LOAD,
  STRING_VARIABLE_TIME_EMPTY
};

const prog_char STRING_DASHES[] PROGMEM = "---";

const byte MAIN_TYPE_TEXT_CELCIUS = 0;
const byte MAIN_TYPE_TEXT_CELCIUS_WITH_NEGATIVE = 1;
const byte MAIN_TYPE_BOX_BLACK_WHEN_TEMP_OVER = 2;
const byte MAIN_TYPE_TEXT_VARIABLE = 3;
const byte MAIN_TYPE_INVERSE_SCALE = 4;

//Table for devices on main page
//{device, x, y, width, height, type, info (when temp over the device number)}
FLASH_TABLE(byte, NUMBER_TEMP_TABLE, 7,
  {DEVICE_LADDNING_RETUR, 99,  5, 10,  7, MAIN_TYPE_TEXT_CELCIUS, 2},
  {DEVICE_LADDNING_UT,    99, 43, 10,  7, MAIN_TYPE_TEXT_CELCIUS, 2},
  {VARIABLE_ID_LOAD,      73,  2, 22, 52, MAIN_TYPE_INVERSE_SCALE, 100},
  {DEVICE_VARMVATTEN,     99, 29, 10,  7, MAIN_TYPE_TEXT_CELCIUS, 2},
  {DEVICE_VARME_RETUR,    55, 33, 10,  7, MAIN_TYPE_TEXT_CELCIUS, 2},
  {DEVICE_VARME_UT,       55, 54, 10,  7, MAIN_TYPE_TEXT_CELCIUS, 2},
  {DEVICE_INNE,           21, 55, 10,  7, MAIN_TYPE_TEXT_CELCIUS, 2},
  {DEVICE_UTE,             0, 55, 15,  7, MAIN_TYPE_TEXT_CELCIUS_WITH_NEGATIVE, 103},
  {VARIABLE_ID_EFFECT,   114, 29, 10,  7, MAIN_TYPE_TEXT_VARIABLE, 2});

/*{DEVICE_TANK1,          74, 51, 20, 6, MAIN_TYPE_BOX_BLACK_WHEN_TEMP_OVER, DEVICE_VARME_UT},
{DEVICE_TANK2,          74, 43, 20, 6, MAIN_TYPE_BOX_BLACK_WHEN_TEMP_OVER, DEVICE_VARME_UT},
{DEVICE_TANK3,          74, 35, 20, 6, MAIN_TYPE_BOX_BLACK_WHEN_TEMP_OVER, DEVICE_VARME_UT},
{DEVICE_TANK4,          74, 27, 20, 6, MAIN_TYPE_BOX_BLACK_WHEN_TEMP_OVER, DEVICE_VARME_UT},
{DEVICE_TANK5,          74, 19, 20, 6, MAIN_TYPE_BOX_BLACK_WHEN_TEMP_OVER, DEVICE_VARME_UT},
{DEVICE_TANK6,          74, 11, 20, 6, MAIN_TYPE_BOX_BLACK_WHEN_TEMP_OVER, DEVICE_VARME_UT},
{DEVICE_TANK7,          74,  3, 20, 6, MAIN_TYPE_BOX_BLACK_WHEN_TEMP_OVER, DEVICE_VARME_UT},*/

//Device, row, column (0 or 1)
FLASH_TABLE(byte, TABLE_VIEW_TABLE, 3,
    {DEVICE_INNE,           0, 0},
    {DEVICE_UTE,            1, 0},
    {DEVICE_VARME_UT,       2, 0},
    {DEVICE_VARME_RETUR,    3, 0},
    {DEVICE_VARMVATTEN,     4, 0},
    {DEVICE_LADDNING_UT,    5, 0},
    {DEVICE_LADDNING_RETUR, 6, 0},
    {VARIABLE_ID_TIME_EMPTY,7, 0},
    {DEVICE_TANK1,          0, 1},
    {DEVICE_TANK2,          1, 1},
    {DEVICE_TANK3,          2, 1},
    {DEVICE_TANK4,          3, 1},
    {DEVICE_TANK5,          4, 1},
    {DEVICE_TANK6,          5, 1},
    {DEVICE_TANK7,          6, 1});

/* ,
    {VARIABLE_ID_LOAD,      7, 1}
 */
const prog_char TEMP_STRING[] PROGMEM = "  ";

//Device, length, offset, resolution (sec)
/*
FLASH_TABLE(byte, HISTORY_TABLE, 6,
    {DEVICE_INNE,        128, 0, 60 , 10,  30},
    {DEVICE_LADDNING_UT, 128, 1, 10 , 0,  100},
    {VARIABLE_ID_EFFECT, 128, 2, 10 , 0,   50},
    {VARIABLE_ID_LOAD,   128, 3, 60 , 0,  100});
*/
//XXX temporary
